from osgeo import gdal,ogr
import sys


def main(dst_path,src_path,shp_path):
    dataset = ogr.Open(shp_path)
    layer = dataset.GetLayer(0)
    # spatialref = layer.GetSpatialRef()
    # extent = layer.GetExtent()
    layer_name = layer.GetName()
    # outputBounds=(extent[0],extent[2],extent[1],extent[3])
    gdal.Warp(
        dst_path, 
        src_path, 
        format="GTiff",
        # outputBoundsSRS=spatialref,
        # outputBounds=outputBounds,
        cutlineDSName=shp_path, # 用于裁剪的矢量
        cutlineLayer=layer_name,
        # cutlineWhere=f"FID={fid}",
        cutlineBlend=True,
        cropToCutline=True, # 是否使用cutlineDSName的extent作为输出的界线
        resampleAlg=gdal.GRIORA_NearestNeighbour,
        # dstNodata=0,
        warpMemoryLimit=1024*1024*1024*100,
        multithread=10,
        callback=gdal.TermProgress_nocb
    )
    dataset.Destroy()
    return


if __name__ == "__main__":
    dst_path=r"/data/fengyy/dateset/temp/0429/test.tif"
    src_path=r"/data/ygjsb/data/Sentinel2tif/41HeNan/L1C_50SKC_20231128-045420.SAFE_0.tif"
    shp_path=r"/data/fengyy/dateset/temp/0429/20240509145314435.shp"
    main(dst_path,src_path,shp_path)
    # main(sys.argv[1],sys.argv[2],sys.argv[3])